package cn.edu.nju.setting;

import java.util.List;

import cn.edu.nju.R;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.TextView;

public class FilterRuleListAdapter extends BaseAdapter{
	private Activity context;
	private SettingProvider provider;
	private List<FilterRule> rules;
	private View invalidView;
	
	public FilterRuleListAdapter(Activity activity){
		this.context = activity;
		this.provider = SettingProvider.getInstance(activity);
		this.rules = provider.getFilterRules();
	}
	
	public int getCount(){
		return rules.size();
	}
	
	public FilterRule getItem(int position){
		return rules.get(position);
	}
	
	public long getItemId(int position){
		return position;
	}
	
	public View getView(int position, View convertView, ViewGroup parent){
		View resultView = null;
		if(convertView == null){
			LayoutInflater inflater = context.getLayoutInflater();
			resultView = inflater.inflate(R.layout.filter_rule_item, null);			
		} else{
			resultView = convertView;
		}
		
		resultView.setTag(R.id.VIEW_POSITION_TAG, position);
		FilterRule rule = getItem(position);
		
		TextView nameView = (TextView)resultView.findViewById(R.id.filter_rule_name);
		nameView.setText(rule.getRuleName());
		
		CheckBox checkBox = (CheckBox)resultView.findViewById(R.id.rule_selection_checker);
		checkBox.setTag(R.id.VIEW_POSITION_TAG, position);
		if(rule.isApplied()){
			checkBox.setChecked(true);
		} else{
			checkBox.setChecked(false);
		}
		
		checkBox.setOnClickListener(new OnClickListener() {	
			public void onClick(View buttonView) {
				int position = (Integer)buttonView.getTag(R.id.VIEW_POSITION_TAG);
				FilterRule rule = getItem(position);
				rule.toggleApplied();
			}
		});
		
		Button actionButton = (Button)resultView.findViewById(R.id.rule_action_button);
		actionButton.setBackgroundResource(R.drawable.ic_expand);
		actionButton.setTag(R.id.EVER_CLICK_TAG, false); // EVER_CLICK_TAG used for lazy init
		actionButton.setTag(R.id.IS_HIDDEN_TAG, true);
		actionButton.setTag(R.id.PARENT_VIEW_TAG, resultView);
		actionButton.setOnClickListener(new OnClickListener() {	
			public void onClick(View v) {
				View parentView = (View)v.getTag(R.id.PARENT_VIEW_TAG);
				View hiddenView = (View)parentView.findViewById(R.id.rule_hidden_layout);
				boolean everClicked = (Boolean) v.getTag(R.id.EVER_CLICK_TAG);
				if(!everClicked){
					int position = (Integer)parentView.getTag(R.id.VIEW_POSITION_TAG);
					FilterRule rule = getItem(position);
					initRuleHiddenPart(parentView, rule);
					v.setTag(R.id.EVER_CLICK_TAG, true);
				} 
				boolean isHidden = (Boolean) v.getTag(R.id.IS_HIDDEN_TAG);
				if(isHidden){
					hiddenView.setVisibility(View.VISIBLE);
					v.setBackgroundResource(R.drawable.ic_collapse);
					v.setTag(R.id.IS_HIDDEN_TAG, false);
				} else{
					hiddenView.setVisibility(View.GONE);
					v.setBackgroundResource(R.drawable.ic_expand);
					v.setTag(R.id.IS_HIDDEN_TAG, true);
				}
			}
		});
		
		// do this because convertView may be already show hidden view
		View hiddenView = (View)resultView.findViewById(R.id.rule_hidden_layout);
		hiddenView.setVisibility(View.GONE);
		return resultView;
	}
	
	public void updateInvalidView(){
		if(invalidView != null){
			int position = (Integer) invalidView.getTag(R.id.VIEW_POSITION_TAG);
			FilterRule rule = getItem(position);
			TextView nameView = (TextView)invalidView.findViewById(R.id.filter_rule_name);
			nameView.setText(rule.getRuleName());
			
			initTagsLayout(invalidView, rule);
			
			invalidView = null;
		}
	}
		
	private void initRuleHiddenPart(View ruleView, FilterRule rule){
		Button modifyButton = (Button)ruleView.findViewById(R.id.modify_rule_button);
		modifyButton.setTag(R.id.PARENT_VIEW_TAG, ruleView);
		modifyButton.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				invalidView = (View)v.getTag(R.id.PARENT_VIEW_TAG);
				int position = (Integer)invalidView.getTag(R.id.VIEW_POSITION_TAG);
				Intent intent = new Intent(context, ModifyRuleActivity.class);
				intent.putExtra(SettingActivity.RULE_ITEM_POSITION, position);
				context.startActivity(intent);
			}
			
		});
		
		Button deleteButton = (Button)ruleView.findViewById(R.id.delete_rule_button);
		deleteButton.setTag(R.id.PARENT_VIEW_TAG, ruleView);
		deleteButton.setOnClickListener(new OnClickListener() {			
			public void onClick(View v) {
				View parentView = (View)v.getTag(R.id.PARENT_VIEW_TAG);
				int position = (Integer)parentView.getTag(R.id.VIEW_POSITION_TAG);
				provider.removeRule(position);
				notifyDataSetChanged();
			}
		});
		
		initTagsLayout(ruleView, rule);
	}
	
	private void initTagsLayout(View ruleView, FilterRule rule){
		ExpandableHeightGridView tagsLayout = (ExpandableHeightGridView) ruleView.findViewById(R.id.filter_tag_layout);
		tagsLayout.setExpanded(true);
		TagAdapter tagAdapter = new TagAdapter(context, rule.getTags());
		tagsLayout.setAdapter(tagAdapter);
	}
}
